package com.test.gae;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.log.Log;

public class ClientServer extends HttpServlet {

	/*private Map<String, String> currPages;*/
	
	private static final Logger log = Logger.getLogger(ClientServer.class.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("UTF-8");
		Writer out = resp.getWriter();
		resp.setContentType("text/xml; charset=UTF-8");
		/*
		 * if (req.getParameter("currPage") != null) {
		 * 
		 * String username = req.getParameter("username"); if (username == null)
		 * { throw new IOException("No username"); } String pageForUser =
		 * currPages.get(username);
		 * 
		 * if (pageForUser == null) { currPages.put(username, "shows.html");
		 * wr.write("shows.html"); } else { wr.write(pageForUser);
		 * 
		 * }
		 * 
		 * } wr.close();
		 */
		out.write("Babe, it's GET");
		out.close();

	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/xml; charset=UTF-8");
		ProtocolParser parser = new ProtocolParser();
		String request = req.getParameter("XMLRequest");
		Writer out = resp.getWriter();
		if (request == null) {
			out.write("<response type=\"error\">What do you want to say??</response>");
		}else {
			
		log.info("The request is:\n"+request);
		String response = parser.handle(request);

		out.write(response);
		}
		out.close();

		/*
		 * String newPage=req.getParameter("newPage"); if (newPage!=null){
		 * currPages.put(req, arg1); }
		 */
	}

}
